package com.sun.aop.handler;

import com.sun.aop.annotation.HandlerType;
import com.sun.aop.util.ClassUtil;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author 喻湘东
 * @Create 2019-09-22 17:25:09
 */
@Component
public class HandlerProcessor implements BeanFactoryPostProcessor {



    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

        Map<String, Class> handlerMap = new ConcurrentHashMap<>();
        ClassUtil.getAllClassByPackageName(AbstractHandler.class.getPackage()).forEach(clazz ->{
            HandlerType handlerType = clazz.getAnnotation(HandlerType.class);
            if(handlerType!=null) {
                String methodName = handlerType.value();
                handlerMap.put(methodName,clazz);
            }
        });
        HandlerContext handlerContext = new HandlerContext(handlerMap);
        beanFactory.registerSingleton(HandlerContext.class.getName(),handlerContext);
    }
}
